package com.verisette.ncos.persistence.domain;

import java.io.Serializable;
import javax.persistence.*;

import org.apache.commons.lang.StringUtils;

import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.web.utilities.M_Utility;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;


/**
 * The persistent class for the TRANS_CUSTOMER_ORDER database table.
 * 
 */
@Entity
@Table(name="TRANS_CUSTOMER_ORDER")
public class TransCustomerOrder implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="ORDER_ID")
	private Integer orderId;

	@Column(name="APPROVED_BY")
	private String approvedBy;

	@Column(name="APPROVED_DATE")
	private Timestamp approvedDate;

	@Column(name="APPROVED_USP_ID")
	private Integer approvedUspId;

	@Column(name="CREATED_BY")
	private String createdBy;

	@Column(name="CREATED_DATE")
	private Timestamp createdDate;

	@Column(name="CREATED_USP_ID")
	private Integer createdUspId;

	@Column(name="EXPORTED_BY")
	private String exportedBy;

	@Column(name="EXPORTED_DATE")
	private Timestamp exportedDate;

	@Column(name="EXPORTED_USP_ID")
	private Integer exportedUspId;

	@Column(name="PDF_PATH")
	private String pdfPath;

	@Column(name="QUANTITY")
	private Integer quantity;

	@Column(name="RECEIVED_BY")
	private String receivedBy;

	@Column(name="RECEIVED_DATE")
	private Timestamp receivedDate;

	@Column(name="RECEIVED_USP_ID")
	private Integer receivedUspId;

	@Column(name="SENT_BY")
	private String sentBy;

	@Column(name="SENT_DATE")
	private Timestamp sentDate;

	@Column(name="SENT_USP_ID")
	private Integer sentUspId;

	@Column(name="STATUS")
	private Integer status;

	@Column(name="UPDATED_BY")
	private String updatedBy;

	@Column(name="UPDATED_DATE")
	private Timestamp updatedDate;

	@Column(name="UPDATED_USP_ID")
	private Integer updatedUspId;
	
	@Column(name="NOT_APPROVED_REASON")
	private String notApprovedReason;

	@Column(name="APPROVED02_BY")
	private String approved02By;

	@Column(name="APPROVED02_DATE")
	private Timestamp approved02Date;

	@Column(name="APPROVED02_USP_ID")
	private Integer approved02UspId;	
	
	@Column(name="PRODUCT_CODE")
	private String productCode;	
	
	@Column(name="PO_PATH")
	private String poPath;	
	
	//bi-directional many-to-one association to MasterMember
	@ManyToOne
	@JoinColumn(name="MMB_ID")
	private MasterMember masterMember;

	//bi-directional many-to-one association to MasterTemplate
	@ManyToOne
	@JoinColumn(name="MTP_ID")
	private MasterTemplate masterTemplate;

	@Column(name="DELIVERY_METHOD")
	private Integer deliveryMethod;
	
	@Column(name="DELIVERY_NAME")
	private String deliveryName;
	
	@Column(name="DELIVERY_ADDR_USE")
	private Integer deliveryAddrUse;//1,2
	
	@Column(name="DELIVERY_ADDR")
	private String deliveryAddr;
	
	@Column(name="DELIVERY_MOBILE")
	private String deliveryMobile;

	@Column(name="PRINT_TYPE")
	private Integer printType;
	
	@Column(name="INVOICE_COMPANY_ID")
	private Integer incoiceCompanyId;
	public Integer getIncoiceCompanyId() {
		return incoiceCompanyId;
	}
	public void setIncoiceCompanyId(Integer incoiceCompanyId) {
		this.incoiceCompanyId = incoiceCompanyId;
	}

	//custom method
	@Transient
	private SimpleDateFormat sdfDisp = new SimpleDateFormat(ProjectConstant.RPT_DATE_FORMAT, ProjectConstant.defaultLocale);
	@Transient
	private SimpleDateFormat sdfOrderNoDisp = new SimpleDateFormat("'MIT'yyMM", ProjectConstant.defaultLocale);
	@Transient
	private NumberFormat nfOrderNoDisp = new DecimalFormat("0000");
	@Transient
	private NumberFormat nfQty = new DecimalFormat("#,##0");
	@Transient
	private NumberFormat nfFloat = new DecimalFormat("#,##0.00");
	
	@Transient
	private final String[] DELIVERY_METHOD_DISP = {
			"",
			"ไปรษณีย์ลงทะเบียน", 
			"ไปรษณีย์ด่วนพิเศษ (EMS)" ,
			"รับสินค้าด้วยตัวเอง" };

	@Transient
	private String[] DELIVERY_ADDR_USE_DESC = {
			"รับสินค้าด้วยตัวเอง",
			"ใช้ที่อยู่ตามนามบัตร",
			"ใช้ที่อยู่ตามที่ระบุ"
	};
	
	@Transient
	private String qty;
	public String getQty() {
		return qty;
	}
	public void setQty(String qty) {
		this.qty = qty;
	}
	
	@Transient private String branchAddrTh;
	public String getBranchAddrTh() {
		return branchAddrTh;
	}
	public void setBranchAddrTh(String branchAddrTh) {
		this.branchAddrTh = branchAddrTh;
	}
	
	@Transient private String txtDeliveryAddrUse;
	public String getTxtDeliveryAddrUse() {
		return txtDeliveryAddrUse;
	}
	public void setTxtDeliveryAddrUse(String txtDeliveryAddrUse) {
		this.txtDeliveryAddrUse = txtDeliveryAddrUse;
	}
	
	@Transient private String mtgId;
	public String getMtgId() {
		return mtgId;
	}
	public void setMtgId(String mtgId) {
		this.mtgId = mtgId;
	}
	
	public String getApproved02DateDisp() {
		return sdfDisp.format(approved02Date);
	}
	
	public String getCreatedDateDisp(){
		return sdfDisp.format(createdDate);
	}
	
	public String getOrderNoDisp(){
		StringBuilder orderNoDispBd = new StringBuilder();
		orderNoDispBd.append(sdfOrderNoDisp.format(createdDate));
		orderNoDispBd.append(nfOrderNoDisp.format(orderId));
		return orderNoDispBd.toString();
	}
	
	public String getOrderOwnerName(){
		StringBuilder orderOwnerNameBd = new StringBuilder();
		orderOwnerNameBd.append("คุณ ")
			.append(masterMember.getMmbFnameTh()).append(" ")
			.append(masterMember.getMmbLnameTh());
		return orderOwnerNameBd.toString();
	}
	
	public String getQtyDisp(){
		return nfQty.format(quantity);
	}
	
	public Float getFinalPricePerUnit()
	{
		System.out.println("############################ "+this.printType);
		if(this.printType==2) return masterTemplate.getMtpPricePerUnit() + 1f;
		return masterTemplate.getMtpPricePerUnit();
	}
	
	public String getPricePerUnitDisp(){
		//return nfFloat.format(masterTemplate.getMtpPricePerUnit());
		return nfFloat.format(getFinalPricePerUnit());
	}
	
	public String getPriceDisp(){
		//return nfFloat.format(quantity * masterTemplate.getMtpPricePerUnit());
		return nfFloat.format(quantity * getFinalPricePerUnit());
	}
	
	public String getPriceIncVatDisp(){
		//return nfFloat.format((quantity * masterTemplate.getMtpPricePerUnit())*1.07);
		return nfFloat.format((quantity * getFinalPricePerUnit())*1.07);
	}
	
	public String getPriceIncVatSpellDisp(){
		StringBuilder priceIncVatSpellBd = new StringBuilder();
		/*
		priceIncVatSpellBd.append("(")
			.append(M_Utility.spell((quantity * masterTemplate.getMtpPricePerUnit())*1.07))
			.append(")");
			*/
		priceIncVatSpellBd.append("(")
		.append(M_Utility.spell((quantity * getFinalPricePerUnit())*1.07))
		.append(")");
		return priceIncVatSpellBd.toString();
	}
	
	public String getSumPriceDisp(){
		//return nfFloat.format(quantity * masterTemplate.getMtpPricePerUnit() + getDiliveryCost());
		return nfFloat.format(quantity * getFinalPricePerUnit() + getDiliveryCost());
	}
	
	public float getDiliveryCost(){
		if(this.quantity==100){
			if(1== this.deliveryMethod) return ProjectConstant.DEFAULT_30;
			if(2== this.deliveryMethod) return 56.00f;
			return 0;
		}
		if(this.quantity==200){
			if(1== this.deliveryMethod) return 40.00f;
			if(2== this.deliveryMethod) return 72.00f;
			return 0;
		}
		if(this.quantity==300){
			if(1== this.deliveryMethod) return 40.00f;
			if(2== this.deliveryMethod) return 72.00f;
			return 0;
		}
		if(this.quantity==400){
			if(1== this.deliveryMethod) return 50.00f;
			if(2== this.deliveryMethod) return 97.00f;
			return 0;
		}
		if(this.quantity==500){
			if(1== this.deliveryMethod) return 50.00f;
			if(2== this.deliveryMethod) return 97.00f;
			return 0;
		}
		return ProjectConstant.DEFAULT_30;
	}
	
	public String getDiliveryCostDisp(){
		return nfFloat.format(getDiliveryCost());
	}
	
	public String getProductCodeDisp(){
		return String.valueOf(this.productCode);
	}
	
	public TransCustomerOrder() {
	}

	public Integer getOrderId() {
		return this.orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public String getApprovedBy() {
		return this.approvedBy;
	}

	public void setApprovedBy(String approvedBy) {
		this.approvedBy = approvedBy;
	}

	public Timestamp getApprovedDate() {
		return this.approvedDate;
	}

	public void setApprovedDate(Timestamp approvedDate) {
		this.approvedDate = approvedDate;
	}

	public Integer getApprovedUspId() {
		return this.approvedUspId;
	}

	public void setApprovedUspId(Integer approvedUspId) {
		this.approvedUspId = approvedUspId;
	}

	public String getCreatedBy() {
		return this.createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public Timestamp getCreatedDate() {
		return this.createdDate;
	}

	public void setCreatedDate(Timestamp createdDate) {
		this.createdDate = createdDate;
	}

	public Integer getCreatedUspId() {
		return this.createdUspId;
	}

	public void setCreatedUspId(Integer createdUspId) {
		this.createdUspId = createdUspId;
	}

	public String getExportedBy() {
		return this.exportedBy;
	}

	public void setExportedBy(String exportedBy) {
		this.exportedBy = exportedBy;
	}

	public Timestamp getExportedDate() {
		return this.exportedDate;
	}

	public void setExportedDate(Timestamp exportedDate) {
		this.exportedDate = exportedDate;
	}

	public Integer getExportedUspId() {
		return this.exportedUspId;
	}

	public void setExportedUspId(Integer exportedUspId) {
		this.exportedUspId = exportedUspId;
	}

	public String getPdfPath() {
		return this.pdfPath;
	}

	public void setPdfPath(String pdfPath) {
		this.pdfPath = pdfPath;
	}

	public Integer getQuantity() {
		return this.quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public String getReceivedBy() {
		return this.receivedBy;
	}

	public void setReceivedBy(String receivedBy) {
		this.receivedBy = receivedBy;
	}

	public Timestamp getReceivedDate() {
		return this.receivedDate;
	}

	public void setReceivedDate(Timestamp receivedDate) {
		this.receivedDate = receivedDate;
	}

	public Integer getReceivedUspId() {
		return this.receivedUspId;
	}

	public void setReceivedUspId(Integer receivedUspId) {
		this.receivedUspId = receivedUspId;
	}

	public String getSentBy() {
		return this.sentBy;
	}

	public void setSentBy(String sentBy) {
		this.sentBy = sentBy;
	}

	public Timestamp getSentDate() {
		return this.sentDate;
	}

	public void setSentDate(Timestamp sentDate) {
		this.sentDate = sentDate;
	}

	public Integer getSentUspId() {
		return this.sentUspId;
	}

	public void setSentUspId(Integer sentUspId) {
		this.sentUspId = sentUspId;
	}

	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getUpdatedBy() {
		return this.updatedBy;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}

	public Timestamp getUpdatedDate() {
		return this.updatedDate;
	}

	public void setUpdatedDate(Timestamp updatedDate) {
		this.updatedDate = updatedDate;
	}

	public Integer getUpdatedUspId() {
		return this.updatedUspId;
	}

	public void setUpdatedUspId(Integer updatedUspId) {
		this.updatedUspId = updatedUspId;
	}

	public MasterMember getMasterMember() {
		return this.masterMember;
	}

	public void setMasterMember(MasterMember masterMember) {
		this.masterMember = masterMember;
	}

	public MasterTemplate getMasterTemplate() {
		return this.masterTemplate;
	}

	public void setMasterTemplate(MasterTemplate masterTemplate) {
		this.masterTemplate = masterTemplate;
	}

	public String getNotApprovedReason() {
		return notApprovedReason;
	}

	public void setNotApprovedReason(String notApprovedReason) {
		this.notApprovedReason = notApprovedReason;
	}

	public String getApproved02By() {
		return approved02By;
	}

	public void setApproved02By(String approved02By) {
		this.approved02By = approved02By;
	}

	public Timestamp getApproved02Date() {
		return approved02Date;
	}

	public void setApproved02Date(Timestamp approved02Date) {
		this.approved02Date = approved02Date;
	}

	public Integer getApproved02UspId() {
		return approved02UspId;
	}

	public void setApproved02UspId(Integer approved02UspId) {
		this.approved02UspId = approved02UspId;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public String getPoPath() {
		return poPath;
	}

	public void setPoPath(String poPath) {
		this.poPath = poPath;
	}
	
	public Integer getDeliveryMethod() {
		return deliveryMethod;
	}
	
	public String getMemberFullNameDisp(){
		String fullName = "";
		if(masterMember != null) {
			StringBuilder sbd = new StringBuilder(masterMember.getMmbFnameTh());
			if(StringUtils.isNotBlank(masterMember.getMmbLnameTh())) {
				sbd.append(" ").append(masterMember.getMmbLnameTh());
			}
			fullName = sbd.toString();
		}
		return fullName;
	}
	
	public String getMemberAddrDisp(){
		String addr = "";
		if(masterMember != null){
			StringBuilder sbd = new StringBuilder(masterMember.getMmbOfficeAddrTh());
			if(StringUtils.isNotBlank(masterMember.getMmbOfficeAddr2Th())){
				sbd.append(" ").append(masterMember.getMmbOfficeAddr2Th());
			}
			addr = sbd.toString();
		}
		return addr;
	}
	
	public String getTemplatePrintDetailDisp(){
		StringBuilder sb = new StringBuilder();
		if(masterTemplate != null) {
			sb.append(masterTemplate.getMtpDescription());
			if(printType != null && printType == 0) {
				sb.append(" ");
				sb.append("พิมพ์หน้าเดียว");
			} else {
				sb.append(" ");
				sb.append("พิมพ์สองหน้า");
			}
		}
		return sb.toString();
	}
	
	@Transient
	public String getDeliveryMethodDisp() {
		return DELIVERY_METHOD_DISP[deliveryMethod==null?0:deliveryMethod];
	}

	public void setDeliveryMethod(Integer deliveryMethod) {
		this.deliveryMethod = deliveryMethod;
	}

	public String getDeliveryName() {
		return deliveryName;
	}
	public void setDeliveryName(String deliveryName) {
		this.deliveryName = deliveryName;
	}
	public String getDeliveryAddrDisp() {
		String deliveryAddrDisp = "";
		if(deliveryAddrUse != null && deliveryAddrUse > 0){
			if(deliveryAddrUse == 1){
				if(masterMember != null){
					StringBuilder sbf = new StringBuilder(masterMember.getMmbOfficeAddrTh());
					if(masterMember.getMmbOfficeAddr2Th() != null && !"".equals(masterMember.getMmbOfficeAddr2Th().trim())){
						sbf.append(" ");
						sbf.append(masterMember.getMmbOfficeAddr2Th());
					}
					deliveryAddrDisp = sbf.toString();
				}
			} else if(deliveryAddrUse == 2){
				if(deliveryAddr != null && !"".equals(deliveryAddr.trim())){
					deliveryAddrDisp = deliveryAddr;
				}
			}
		}		
		return deliveryAddrDisp;
	}
	public String getDeliveryAddr() {
		return deliveryAddr;
	}
	public void setDeliveryAddr(String deliveryAddr) {
		this.deliveryAddr = deliveryAddr;
	}
	public String getDeliveryMobile() {
		return deliveryMobile;
	}
	public void setDeliveryMobile(String deliveryMobile) {
		this.deliveryMobile = deliveryMobile;
	}
	public Integer getPrintType() {
		return printType;
	}
	public void setPrintType(Integer printType) {
		this.printType = printType;
	}
	
	public String getDeliveryAddrUseDisp(){
		return DELIVERY_ADDR_USE_DESC[deliveryAddrUse==null?0:deliveryAddrUse];
	}
	
	public Integer getDeliveryAddrUse() {
		return deliveryAddrUse;
	}
	public void setDeliveryAddrUse(Integer deliveryAddrUse) {
		this.deliveryAddrUse = deliveryAddrUse;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("TransCustomerOrder [");
		if (orderId != null) {
			builder.append("orderId=");
			builder.append(orderId);
			builder.append(", ");
		}
		if (approvedBy != null) {
			builder.append("approvedBy=");
			builder.append(approvedBy);
			builder.append(", ");
		}
		if (approvedDate != null) {
			builder.append("approvedDate=");
			builder.append(approvedDate);
			builder.append(", ");
		}
		if (approvedUspId != null) {
			builder.append("approvedUspId=");
			builder.append(approvedUspId);
			builder.append(", ");
		}
		if (createdBy != null) {
			builder.append("createdBy=");
			builder.append(createdBy);
			builder.append(", ");
		}
		if (createdDate != null) {
			builder.append("createdDate=");
			builder.append(createdDate);
			builder.append(", ");
		}
		if (createdUspId != null) {
			builder.append("createdUspId=");
			builder.append(createdUspId);
			builder.append(", ");
		}
		if (exportedBy != null) {
			builder.append("exportedBy=");
			builder.append(exportedBy);
			builder.append(", ");
		}
		if (exportedDate != null) {
			builder.append("exportedDate=");
			builder.append(exportedDate);
			builder.append(", ");
		}
		if (exportedUspId != null) {
			builder.append("exportedUspId=");
			builder.append(exportedUspId);
			builder.append(", ");
		}
		if (pdfPath != null) {
			builder.append("pdfPath=");
			builder.append(pdfPath);
			builder.append(", ");
		}
		if (quantity != null) {
			builder.append("quantity=");
			builder.append(quantity);
			builder.append(", ");
		}
		if (receivedBy != null) {
			builder.append("receivedBy=");
			builder.append(receivedBy);
			builder.append(", ");
		}
		if (receivedDate != null) {
			builder.append("receivedDate=");
			builder.append(receivedDate);
			builder.append(", ");
		}
		if (receivedUspId != null) {
			builder.append("receivedUspId=");
			builder.append(receivedUspId);
			builder.append(", ");
		}
		if (sentBy != null) {
			builder.append("sentBy=");
			builder.append(sentBy);
			builder.append(", ");
		}
		if (sentDate != null) {
			builder.append("sentDate=");
			builder.append(sentDate);
			builder.append(", ");
		}
		if (sentUspId != null) {
			builder.append("sentUspId=");
			builder.append(sentUspId);
			builder.append(", ");
		}
		if (status != null) {
			builder.append("status=");
			builder.append(status);
			builder.append(", ");
		}
		if (updatedBy != null) {
			builder.append("updatedBy=");
			builder.append(updatedBy);
			builder.append(", ");
		}
		if (updatedDate != null) {
			builder.append("updatedDate=");
			builder.append(updatedDate);
			builder.append(", ");
		}
		if (updatedUspId != null) {
			builder.append("updatedUspId=");
			builder.append(updatedUspId);
			builder.append(", ");
		}
		if (notApprovedReason != null) {
			builder.append("notApprovedReason=");
			builder.append(notApprovedReason);
			builder.append(", ");
		}
		if (approved02By != null) {
			builder.append("approved02By=");
			builder.append(approved02By);
			builder.append(", ");
		}
		if (approved02Date != null) {
			builder.append("approved02Date=");
			builder.append(approved02Date);
			builder.append(", ");
		}
		if (approved02UspId != null) {
			builder.append("approved02UspId=");
			builder.append(approved02UspId);
			builder.append(", ");
		}
		if (productCode != null) {
			builder.append("productCode=");
			builder.append(productCode);
			builder.append(", ");
		}
		if (poPath != null) {
			builder.append("poPath=");
			builder.append(poPath);
			builder.append(", ");
		}
		if (masterMember != null) {
			builder.append("masterMember=");
			builder.append(masterMember);
			builder.append(", ");
		}
		if (masterTemplate != null) {
			builder.append("masterTemplate=");
			builder.append(masterTemplate);
			builder.append(", ");
		}
		if (sdfDisp != null) {
			builder.append("sdfDisp=");
			builder.append(sdfDisp);
			builder.append(", ");
		}
		if (sdfOrderNoDisp != null) {
			builder.append("sdfOrderNoDisp=");
			builder.append(sdfOrderNoDisp);
			builder.append(", ");
		}
		if (nfOrderNoDisp != null) {
			builder.append("nfOrderNoDisp=");
			builder.append(nfOrderNoDisp);
			builder.append(", ");
		}
		if (nfQty != null) {
			builder.append("nfQty=");
			builder.append(nfQty);
			builder.append(", ");
		}
		if (nfFloat != null) {
			builder.append("nfFloat=");
			builder.append(nfFloat);
			builder.append(", ");
		}
		if (deliveryMethod != null) {
			builder.append("deliveryMethod=");
			builder.append(deliveryMethod);
			builder.append(", ");
		}
		if (deliveryName != null) {
			builder.append("deliveryName=");
			builder.append(deliveryName);
			builder.append(", ");
		}
		if (deliveryAddr != null) {
			builder.append("deliveryAddr=");
			builder.append(deliveryAddr);
			builder.append(", ");
		}
		if (deliveryAddrUse != null) {
			builder.append("deliveryAddrUse=");
			builder.append(deliveryAddrUse);
			builder.append(", ");
		}
		if (deliveryMobile != null) {
			builder.append("deliveryMobile=");
			builder.append(deliveryMobile);
			builder.append(", ");
		}
		if (printType != null) {
			builder.append("printType=");
			builder.append(printType);
		}
		builder.append("]");
		return builder.toString();
	}
}